package cn.xingyun.pickaxe_loli_new.command;

import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.SharedSuggestionProvider;
import net.minecraft.commands.arguments.GameModeArgument;
import net.minecraft.network.chat.Component;

import java.util.Arrays;
import java.util.Collection;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class MyArgument implements ArgumentType<ModeType> {
    private static final Collection<String> EXAMPLES = Stream.of(ModeType.NOSCREEN, ModeType.NOSPAWN).map(ModeType::getName).collect(Collectors.toList());
    private static final ModeType[] VALUES = ModeType.values();
    private static final DynamicCommandExceptionType ERROR_INVALID = new DynamicCommandExceptionType((p_260119_) -> {
        return Component.translatable("argument.gamemode.invalid", p_260119_);
    });

    public ModeType parse(StringReader p_260111_) throws CommandSyntaxException {
        String s = p_260111_.readUnquotedString();
        ModeType modetype = ModeType.byName(s, (ModeType)null);
        if (modetype == null) {
            throw ERROR_INVALID.createWithContext(p_260111_, s);
        } else {
            return modetype;
        }
    }

    public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> p_259767_, SuggestionsBuilder p_259515_) {
        return p_259767_.getSource() instanceof SharedSuggestionProvider ? SharedSuggestionProvider.suggest(Arrays.stream(VALUES).map(ModeType::getName), p_259515_) : Suggestions.empty();
    }

    public Collection<String> getExamples() {
        return EXAMPLES;
    }

    public static MyArgument gameMode() {
        return new MyArgument();
    }

    public static ModeType getGameMode(CommandContext<CommandSourceStack> p_259927_, String p_260246_) throws CommandSyntaxException {
        return p_259927_.getArgument(p_260246_, ModeType.class);
    }
}
